<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\admin\exception\MenuException;
use app\admin\model\SystemMenu;
use app\admin\validate\IDMustBePositiveInt;
use app\admin\validate\NavigationValidate;
use think\facade\View;
use think\Request;

class Navigation extends Base
{

    public function index()
    {
        $parentMenu =  SystemMenu::getParentMenu();
        View::assign([
            'lists' => $parentMenu->toArray()
        ]);
        return View::fetch();
    }

    /**
     * @Descripttion:
     * @param Request $request
     * @return string|\think\response\Json
     * @throws \app\lib\exception\ParameterException
     * @author Siebe
     * @date 2022-02-19 1:10
     */
    public function add(Request $request)
    {
        if($request->isPost()){
            (new NavigationValidate())->goCheck(NavigationValidate::Save);
            $params = $request->param();
            $params['pid'] = 0;
            $params['type'] = 0;
            $params['show'] = 1;
            SystemMenu::create($params,['pid','name','icon','type','sort','status','show']);
            return json([
                'msg' => '保存成功',
                'error_code' => 0
            ],200);
        }else{
            return View::fetch();
        }

    }

    /**
     * @Descripttion:
     * @param Request $request
     * @param $id
     * @return string|\think\response\Json
     * @throws MenuException
     * @throws \app\lib\exception\ParameterException
     * @author Siebe
     * @date 2022-02-19 1:09
     */
    public function edit(Request $request, $id)
    {
        (new IDMustBePositiveInt)->goCheck('check');
        $menu = SystemMenu::getParentMenuByID($id);
        if(!$menu){
            throw new MenuException([
                'code' => 404,
                'msg' => '请传入导航菜单ID',
                'errorCode' => 10302
            ]);
        }

        if($request->isPost()){
            (new NavigationValidate())->goCheck(NavigationValidate::Update);
            $params = $request->param();
            $res = $menu->save($params);
            if(!$res){
                throw new MenuException([
                    'code' => 404,
                    'msg' => '修改导航失败',
                    'errorCode' => 10303
                ]);
            }
            return json([
                'msg' => '更新成功',
                'error_code' => 0
            ],200);
        }else{
            View::assign([
                'menu' => $menu->toArray()
            ]);
            return View::fetch();
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        (new IDMustBePositiveInt)->goCheck('check');
        $menu = SystemMenu::find($id);
        if(!$menu){
            throw new MenuException([
                'code' => 404,
                'msg' => '请传入导航菜单ID',
                'errorCode' => 10302
            ]);
        }
        //删除导航时要判断该导航下面是否存在子菜单

        if(!$menu->delete()){
            throw new MenuException([
                'code' => 404,
                'msg' => '删除导航失败',
                'errorCode' => 10304
            ]);
        }
        return json([
            'msg' => '删除成功',
            'error_code' => 0
        ],200);
    }
}
